from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

path = Service('./chromedriver')
driver = webdriver.Chrome(service=path)
# 打开豆瓣排行榜
driver.get('https://movie.douban.com/chart')
# time.sleep(2)
# driver.implicitly_wait(20)  # 隐式等待
driver.maximize_window()  # 最大化窗口
driver.save_screenshot('./豆瓣排行榜.png')  # 截图豆瓣排行榜页面
# <a class="nbg" href="https://movie.douban.com/subject/35882838/"  title="某种物质">
# 显示等待
movie = WebDriverWait(driver, 20, 0.5).until(lambda driver: driver.find_elements(By.CLASS_NAME, 'nbg'))
# 定位电影名所在的元素
# movie = driver.find_elements(By.CLASS_NAME, 'nbg')
# < span class ="rating_nums" > 7.5 < / span >
# 定位分数所在的元素
score = driver.find_elements(By.CLASS_NAME, 'rating_nums')

movie_info = []
for i in range(len(movie)):
    m = movie[i].get_attribute('title')  # 电影名
    s = score[i].text  # 分数
    movie_info.append((m, s))

print(movie_info)

driver.quit()
